
import axios from 'axios'
import { message } from 'ant-design-vue'
// import router from '../router'
// import store from '../store'

// 创建axios实例
const instance = axios.create({
  baseURL: '/chrome-api/',
  timeout: 18000
})
// 设置请求头
// instance.defaults.headers['Content-Type'] = 'application/json; charset=UTF-8'
/**
 * 请求拦截器
 * 每次请求前，如果存在token则在请求头中携带token
 */
const isDev = process.env.NODE_ENV === 'development'
instance.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('Access-Token') // store.getters['user/setAccess_Token'] // localStorage.getItem('Access_Token')
    localStorage.setItem('Access-Token', 'eyJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE2NDMxNjkxOTYsImxvZ2luX3VzZXJfa2V5IjoiMTYyMTkzMTA1MDUyMzY5NTgifQ.4A3J5MVdHXuXPeuC7vViEADrbT6YFgF8tTpzLB-lsulCvnVqjcod3pRwd61cwbNTkRHUt61tkUPfi7sJiokAwQ')
    config.headers!['Portal-Token'] = 'eyJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE2NDMxNjkxOTYsImxvZ2luX3VzZXJfa2V5IjoiMTYyMTkzMTA1MDUyMzY5NTgifQ.4A3J5MVdHXuXPeuC7vViEADrbT6YFgF8tTpzLB-lsulCvnVqjcod3pRwd61cwbNTkRHUt61tkUPfi7sJiokAwQ'
      // config.headers['Portal-Token'] = 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjAwMDI5In0.U-foActUKUdNuMCBFNEGalgXxCH3vhotuamRzxfQBLKLe1gtKxAk_jj6djNVMBmRf8XKmPkfGGg8n5LRO_wmig'
    
    return config
  },
  (error) => Promise.reject(error)
)

// 响应拦截器
instance.interceptors.response.use(
  // 请求成功
  (res) => {
    if (res.status === 200) {
      return Promise.resolve(res.data)
    } else {
      message.error('')
      return Promise.reject(res)
    }
  },
  // 请求失败
  (error) => {
    const { response } = error
    if (response) {
      // 请求已发出，但是不在2xx的范围
      if (response.data.errorMessage) {
        if (response.status === 500) {
          if (response.data.code === 402) {
            // 禁用
            message.error(response)
          } else {
            message.error(response.data.errorMessage)
          }
        } else if (response.status === 403) {
          // 无权限
          message.error(response.data.errorMessage)
        } else {
          message.error(response)
        }
      }
      return Promise.reject(response)
    }
  }
)

export default instance
